package zuoye4;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.util.Arrays;
public class RegexExamples {

    public static void main(String[] args) {
        validateEmail();
        findAllDigits();
        extractNamedGroups();
        replaceExample();
        splitExample();
        demoFlags();
    }

    // 验证邮箱（简化）
    static void validateEmail() {
        String regex = "^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$";
        Pattern p = Pattern.compile(regex);
        String[] tests = {
            "alice@example.com",
            "bob.smith+tag@sub.domain.co",
            "invalid@@example.com",
            "no-at-sign.com"
        };
        System.out.println("Email 验证:");
        for (String t : tests) {
            boolean ok = p.matcher(t).matches();
            System.out.printf("  %s => %s%n", t, ok ? "valid" : "invalid");
        }
        System.out.println();
    }

    // 使用 find() 查找所有数字子串
    static void findAllDigits() {
        String input = "Order 123: price 45.67, Order 124: price 89.00";
        Pattern p = Pattern.compile("\\d+(?:\\.\\d+)?"); // 匹配整数或小数
        Matcher m = p.matcher(input);
        System.out.println("查找所有数字:");
        while (m.find()) {
            System.out.printf("  match '%s' at [%d,%d)%n", m.group(), m.start(), m.end());
        }
        System.out.println();
    }

    // 命名捕获组示例（Java 支持 (?<name>...)）
    static void extractNamedGroups() {
        String input = "Name: Alice, Age: 30; Name: Bob, Age: 25";
        Pattern p = Pattern.compile("Name:\\s*(?<name>[^,;]+),\\s*Age:\\s*(?<age>\\d+)");
        Matcher m = p.matcher(input);
        System.out.println("提取命名捕获组:");
        while (m.find()) {
            String name = m.group("name");
            String age = m.group("age");
            System.out.printf("  name=%s, age=%s%n", name.trim(), age);
        }
        System.out.println();
    }

    // replaceAll 示例：将连续空白归一化
    static void replaceExample() {
        String s = "This   is\tan  example\nwith   mixed   whitespace.";
        String normalized = s.replaceAll("\\s+", " ");
        System.out.println("替换示例:");
        System.out.println("  原始: " + s);
        System.out.println("  结果: " + normalized);
        System.out.println();
    }

    // split 示例：按逗号分割（忽略逗号周围空白）
    static void splitExample() {
        String csv = "apple, banana ,cherry,  date";
        String[] parts = csv.split("\\s*,\\s*");
        System.out.println("split 示例:");
        System.out.println("  " + Arrays.toString(parts));
        System.out.println();
    }

    // 演示 Pattern flags（CASE_INSENSITIVE, MULTILINE, DOTALL）
    static void demoFlags() {
        String text = "First line\nSecond Line";
        Pattern p1 = Pattern.compile("^second", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
        Matcher m1 = p1.matcher(text);
        System.out.println("flags 示例:");
        while (m1.find()) {
            System.out.printf("  Found '%s' at [%d,%d)%n", m1.group(), m1.start(), m1.end());
        }

        String dotText = "a\nb";
        Pattern p2 = Pattern.compile("a.*b"); // 默认 . 不匹配换行，无法匹配
        Pattern p3 = Pattern.compile("a.*b", Pattern.DOTALL); // . 匹配换行
        System.out.println("  DOTALL match without flag: " + p2.matcher(dotText).matches());
        System.out.println("  DOTALL match with flag:    " + p3.matcher(dotText).matches());
        System.out.println();
    }
}
